package com.cwj.we.module.bizhi;

import android.content.Intent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cwj.we.R;
import com.cwj.we.base.BaseActivity;
import com.cwj.we.base.BaseRVAdapter;
import com.cwj.we.base.BaseRVHolder;
import com.cwj.we.bean.GetDataType;
import com.cwj.we.bean.VerticalBiZhi;
import com.cwj.we.utils.LoadingDialog;
import com.cwj.we.utils.ToastUtil;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;

public class BiZhiActivity extends BaseActivity<BiZhiPrensenter> implements BiZhiView {

    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    @BindView(R.id.rv)
    RecyclerView rv;
    private BaseRVAdapter<VerticalBiZhi.ResBean.CategoryBean> adapter;
    private LoadingDialog loadingDialog;

    @Override
    protected BiZhiPrensenter createPresenter() {
        return new BiZhiPrensenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bi_zhi;
    }

    @Override
    public void initData() {
        loadingDialog.show();
        presenter.getVertical(GetDataType.GETDATA);

        adapter = new BaseRVAdapter<VerticalBiZhi.ResBean.CategoryBean>(R.layout.item_type) {
            @Override
            public void onBindVH(BaseRVHolder holder, VerticalBiZhi.ResBean.CategoryBean data, int position) {
                LinearLayout ll = holder.getView(R.id.ll);
                ImageView iv = holder.getView(R.id.iv);
                TextView tvName = holder.getView(R.id.tvName);
                Glide.with(BiZhiActivity.this)
                        .load(data.getCover())
                        .into(iv);
                tvName.setText(data.getName());
                //点击事件
                ll.setOnClickListener(view -> {
                    Intent intent = new Intent(BiZhiActivity.this, BiZhiTypeActivity.class);
                    intent.putExtra("id", data.getId());
                    startActivity(intent);
                });
            }
        };
        //禁用加载更多
        srl.setEnableLoadMore(false);
        //下拉刷新
        srl.setOnRefreshListener(refreshLayout -> {
            presenter.getVertical(GetDataType.REFRESH);
        });
        //设置适配器
        rv.setAdapter(adapter);
        rv.setLayoutManager(new GridLayoutManager(this, 2));//列数设置
    }

    @Override
    public void initView() {
        ImmersionBar.with(this)
                .statusBarDarkFont(true)  //状态栏字体是深色，不写默认为亮色
                .init();
        loadingDialog = new LoadingDialog(this, "加载中...");
    }

    @Override
    public void onError(String msg) {
        loadingDialog.dismiss();
        ToastUtil.showTextToast(this, msg);
        srl.finishRefresh();
        srl.finishLoadMore();
    }

    @Override
    public void getVerticalSuccess(VerticalBiZhi bean, Integer type) {
        loadingDialog.dismiss();
        if (bean != null && bean.getRes() != null && bean.getRes().getCategory() != null) {
            switch (type) {
                case GetDataType.GETDATA://获取数据成功
                    adapter.setNewData(bean.getRes().getCategory());
                    break;
                case GetDataType.REFRESH://刷新成功
                    adapter.setNewData(bean.getRes().getCategory());
                    srl.finishRefresh();
                    srl.setNoMoreData(false);
                    break;
            }
        }
    }
}